package com.example.qxapp.Fragment;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public abstract class basefragment extends Fragment {
    public Context context;
    private View rootview;//缓存的frag
    public Resources resources;
    public LayoutInflater layoutInflater;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.context=context;
        this.resources=context.getResources();
        this.layoutInflater=LayoutInflater.from(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootview==null){
            rootview=inflater.inflate(getlayid(),container,false);
        }

        ViewGroup parent= (ViewGroup) rootview.getParent();
        if (parent!=null){
            parent.removeView(rootview);
        }
        return rootview;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initview(view);
        bindEvent();
        initData();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    //VV为view类型 id 控件id
    protected <VV extends View> VV findview(View view, @IdRes int id){
        return view.findViewById(id);
    };

    protected <VV extends View> VV findview( @IdRes int id){
        return rootview.findViewById(id);
    };

    protected abstract void initData();

    protected abstract void bindEvent();

    protected abstract void initview(View view);

    protected abstract int getlayid();
}
